<?php

namespace common\modules\wechat\models;

use common\components\ActiveQuery;

class ReplyKeywordQuery extends ActiveQuery
{

    /**
     * 文本类型关键字过滤
     *
     * @param $keyword
     * @return $this
     */
    public function keyword($keyword)
    {
        $this->andWhere([
            'or',
            ['and', '{{type}}=:typeMatch', '{{keyword}}=:keyword'], // 直接匹配关键字
            ['and', '{{type}}=:typeInclude', 'INSTR(:keyword, {{keyword}})>0'], // 包含关键字
            ['and', '{{type}}=:typeRegular', ':keyword REGEXP {{keyword}}'], // 正则匹配关键字
        ])->addParams([
            ':keyword'     => $keyword,
            ':typeMatch'   => ReplyKeyword::TYPE_MATCH,
            ':typeInclude' => ReplyKeyword::TYPE_INCLUDE,
            ':typeRegular' => ReplyKeyword::TYPE_REGULAR,
        ]);

        return $this;
    }

    /**
     * 查询公众号规则
     *
     * @param int $wid
     * @param int $status
     * @return $this
     */
    public function wechatRule($wid, $status = ReplyRule::STATUS_ACTIVE)
    {
        $this->joinWith([
            'rule' => function ($query) use ($wid, $status) {
                /** @var ReplyRuleQuery $query */
                if ($status !== null)
                {
                    $query->active($status);
                }
                $query->andWhere(['wid' => $wid]);
            },
        ]);
        return $this;
    }

    /**
     * 过滤有效时间
     *
     * @param $time
     * @return $this
     */
    public function limitTime($time)
    {
        $this->andWhere([
            'and',
            ['or', 'start_at<:time', 'start_at=0'],
            ['or', 'end_at>:time', 'end_at=0'],
        ])->addParams([
            ':time' => $time,
        ]);

        return $this;
    }
}
